package com.pro01.backend.mapper;

import com.pro01.backend.pojo.PointLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PointLogMapper {

    // 登录增加积分
    @Select("""
    SELECT COUNT(*) FROM point_logs
    WHERE user_id = #{userId}
      AND source = 'LOGIN'
      AND DATE(created_at) = CURDATE()
""")
    int countTodayLoginPoints(@Param("userId") Long userId);

    @Insert("""
    INSERT INTO point_logs (user_id, change_type, points, source, description, created_at)
    VALUES (#{userId}, #{changeType}, #{points}, #{source}, #{description}, NOW())
""")
    int insertPointLog(PointLog log);

    // 查看积分明细
    @Select("""
    SELECT * FROM point_logs
    WHERE user_id = #{userId}
    ORDER BY created_at DESC
    LIMIT #{offset}, #{pageSize}
""")
    List<PointLog> getLogsByUser(@Param("userId") Long userId,
                                 @Param("offset") int offset,
                                 @Param("pageSize") int pageSize);

    @Select("""
    SELECT COUNT(*) FROM point_logs
    WHERE user_id = #{userId}
""")
    int countLogsByUser(@Param("userId") Long userId);

}
